home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6109 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.4 KB

  1. Path: qualcomm.com!usenet
  2. From: nababs@qualcomm.com (Nasser Abbasi)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: What's so bad about global variables?
  5. Date: 11 Feb 1996 10:14:44 GMT
  6. Organization: Qualcomm Inc.
  7. Message-ID: <4fkfik$b1g@qualcomm.com>
  8. References: <4fjtga$a8s@ias2.ichange.com>
  9. NNTP-Posting-Host: nabbasi.qualcomm.com
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.93.14
  12.  
  13. In article <4fjtga$a8s@ias2.ichange.com>, jshute@connect.reach.net 
  14. says...
  15. >
  16. >
  17. > Why do programmers treat like global variables as if they are the
  18. >single most evil thing you can possibly do in a C program?
  19. >
  20.  
  21.  
  22. Well, I think the reason of not desiring to use a global variable 
  23. comes from the idea that reducing coupling between different functions
  24. in different modules in a large program leads to a cleaner program to 
  25. debug and maintain,and that the only interaction between different 
  26. functions in a system should be through parameter passing (in OO terms, 
  27. called message passing, when the functions are part of a class object).
  28.  
  29. A global variable is a means for achieving fast exchange of information 
  30. between different parts of a program, and as we all know, there is no 
  31. free lunch in this world, one must pay a price for this, what is this 
  32. price?
  33.  
  34. 2 different functions in different modules sharing a global variables 
  35. are coupled with each others in an undesirable and uncontrolled way, the 
  36. code inside one function becomes dependent on the form/structure and the 
  37. means of access of this global object, any future needs by one function 
  38. to modify some of these attribute of the shared object could also affect 
  39. the rest of program, different functions in different parts of the 
  40. program might have to change because one function needed to change its 
  41. own internal way of using the global object. (certainly something we do 
  42. not want to happen, we have coupled the way one function internally 
  43. perform processing on a global variable with the way other functions in 
  44. the system does its own internal processing on the same variable, a bad 
  45. side effect of sharing the use of global variables).
  46.  
  47. Also if one function wants to change the protocol by which it 
  48. uses/access this global variables, all other parts of the program might 
  49. be affected. One must start looking through the source code looking for 
  50. all places where this global variable is being used, which could be 
  51. spread all over the place, in addition one must try to figure if the 
  52. access is read/write/update by the context, this is a messy and 
  53. unorganized way of developing software, there is not one place can look 
  54. at to determine the access mechanism, compare that with argument passing 
  55. (message passing) where one knows by looking at the function definition 
  56. what type of access the arguments are being passed as. (in Ada terms, IN, 
  57. IN OUT, OUT), in C++ one can apply the access modifier "const" 
  58. to the parameters, assuming no one is casting the constness way).
  59.  
  60. Another problem with using a shared variable shows up in multithreaded 
  61. applications, when the global variable has to be protected by a critical 
  62. section else disorder might rule. (this might not have to be done, it
  63. depends on the application use of the global variable),  Look for example 
  64. at the  problem in using errno for example in multithreaded applications 
  65. in C.
  66.  
  67. Not all uses of global variables are bad, It depends on the scope of the 
  68. global variable,  In C terms, having a static variable inside a file 
  69. declared outside any function in the file (i.e at outer level) gives that 
  70. variable a global scope over all the functions inside that file only, but 
  71. that variable is still hidden from the rest of the program, functions in 
  72. different files/modules can not access this variable, this is just fine, 
  73. since functions inside that one file are supposed to have strong cohesion 
  74. between them, they by definition should be cooperating functions, and so 
  75. can all coordinate accessing this file level global variable. (This is 
  76. similar to methods inside one class having access to private members of 
  77. the class).
  78.  
  79. Also, many times in system programming, shared memory sections are used 
  80. for fast communications between different processes, (this is considered 
  81. one of the fastest way to exchange data between processes), this is 
  82. another form of global variable use, but at the process level, here also 
  83. additional code must be written so that controlled and synchronized 
  84. access to this shared memory is achieved, usually by using system 
  85. services.
  86.  
  87. One might say, but I have a variable that a higher level routine has 
  88. updated, and a low level routine needs to access, and passing this down 
  89. through few layers of software until it reaches the low level routine is 
  90. a waste of resources and time, when one can just update a global area of 
  91. memory, and let the low level routine just read it directly, resulting is 
  92. speed of execution.
  93.  
  94. Well, for me, this is sacrificing design integrity of a program for sake 
  95. of immediate gain of speed, there is most likely a better design where 
  96. this need can be removed, I also think that one must first come up with a 
  97. clean design, then worry about performance issues after that, as much as 
  98. possible.
  99.  
  100. Bad effects of using global variables shows up most in large programs, in 
  101. small program, one might be able to use program level global variables 
  102. and not see any bad side effect to its use, but it is a good idea to 
  103. start with the habit of not using global variable even in small programs 
  104. (less that 5 to 10 K lines).
  105.  
  106. IMHO 
  107.  
  108. Nasser
  109.  
  110.